#!/usr/bin/env sh

DEVICE=${1}
ACTION=${2}

if [ -z "$DEVICE" ]; then
	echo "Device is not supplied"
	return 1
fi

# Get status
DEVICE_STATUS=$(bluetoothctl info "$DEVICE" | grep Connected | awk '{print $2}')

if [ -z "$DEVICE_STATUS" ]; then
	echo "$FAIL"
	notify-send "Failed to get ${DEVICE} status"
	return 1
fi

case $ACTION in
	connect)
		case $DEVICE_STATUS in
			yes)
				notify-send "$DEVICE is already connected" ;;
			no)
				notify-send "Attempting to connect $DEVICE..." && bluetoothctl connect "$DEVICE" > /dev/null 2>&1 ;;
		esac ;;
	disconnect)
		case $DEVICE_STATUS in
			yes)
				notify-send "Disconnected $DEVICE" && bluetoothctl disconnect "$DEVICE" > /dev/null 2>&1 & ;;
			no)
				notify-send "$DEVICE is already disconnected" ;;
		esac ;;
esac

echo "$DEVICE_STATUS"
